<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/** 
 * load the input variable with data
 * @arg $input_data string of an string of input
 * 		$input_data should be in following format..
 *		array( 	"pg_var1" => var_value for var1,
 *				"pg_var2" => var_value for var2,
 *				"dt_var3" => indexes of the data variable var3,
 *				"dt_var4" => "1-10,11,12,13,14,15,16,20-29");
 * @return 0 on success, -1 on failure..
 */
function load_input_vars_from_data($input_data)
{
	$input_vars = array("page"=>array(),
	                    "table"=>array(),
						"sys"=>array()
				 );
				
	$keys = array_keys($input_data);
	
	foreach ($keys as $key)
	{
		if(!strncmp($key, "pg_",3))
		{
			$var_name = substr($key, 3);
			$value = $input_data[$key];
			$input_vars["page"][$var_name] = $value;
		}
		if(!strncmp($key, "sy_",3))
		{
			$var_name = substr($key, 3);
			$value = $input_data[$key];
			$input_vars["sys"][$var_name] = $value;
		}
		elseif(!strncmp($key, "dt_",3))
		{
			$var_name = substr($key, 3);
			$value = array();
			
			
			$string = $input_data[$key];
			$tok = strtok($string, ",");
			
			
			while ($tok !== false)
			{
				$this_value = $tok; //to find the last token.
				list($from, $to) = split('[-]', $this_value);
				if( $to == NULL) //only one number is there..
				{
					$value[$from] = array('id'=> $from);
				}
				else
				{
					for($i = $from; $i <= $to; $i ++)
					{
						$value[$i] = array('id'=> $i);
					}
				}
			   	$tok = strtok(",");
			}
			$input_vars["table"][$var_name] = $value;
		}
	}
	//print_r($input_vars);
	return $input_vars;
}
/** 
 * retrieve the difference of input_vars and output_vars
 * @arg input_vars input variable..
 * @arg output_vars output variable..
 * @return the difference of given xml..
 */
function echo_differences_of($input_vars, $output_vars, $just_loaded)
{
	$diff_vars = array("page" => array(),
	                    "table" => array(),
						"sys"=> array()
					);
	if(is_array($output_vars["page"]))
	{			
		$page_vars = array_keys($output_vars["page"]); //first turn out page variables
		foreach($page_vars as $page_var)
		{
			// $page_var represent the page variables..
			$input_val = $input_vars["page"][$page_var];
			$output_val = $output_vars["page"][$page_var];
			if( !isset($input_val) ||  $input_val != $output_val  || $just_loaded)
			{
				$diff_vars["page"][$page_var] = $output_vars["page"][$page_var]; 
				//just set to output var..
			}
		}
	}
		
	if(is_array($output_vars["sys"]))
	{			
		$page_vars = array_keys($output_vars["sys"]); //simmilar to page variables
		foreach($page_vars as $page_var)
		{
			if($page_var == "temp_params")//ignore this system variable for publishing..
			{
				continue;
			}
			// $page_var represent the page variables..
			$input_val = $input_vars["sys"][$page_var];
			$output_val = $output_vars["sys"][$page_var];
			if( !isset($input_val) ||  $input_val != $output_val  || $just_loaded)
			{
				if($output_val != NULL)
				{
					$diff_vars["sys"][$page_var] = $output_vars["sys"][$page_var];
				}
				//just set to output var..
			}
		}
	}
					
	if(is_array($output_vars["table"]))
	{		
		// find the diff of table :(
		// set the value: 
		// 0 - keep the same..
		// 1 - new one..
		$data_vars = array_keys($output_vars["table"]);
		foreach($data_vars as $data_var)
		{
			$input_val = $input_vars["table"][$data_var];
			$output_val = $output_vars["table"][$data_var];
			$diff_vars["table"][$data_var] = array();
	
			$ids = array_keys($output_val);
			
			//check whether new outputs are available..
			foreach($ids as $id)
			{
				if ($input_val[$id] != NULL)
				{
					// so no need to change..
					$diff_vars["table"][$data_var][$id] = 0;
				}
				else
				{
					$diff_vars["table"][$data_var]["changed"] = 1;
					$diff_vars["table"][$data_var][$id] = 1;
				}
			}
			
			//check_whether some inputs are deleted..
			if( $diff_vars["table"][$data_var]["changed"] == NULL)
			{
				if(is_array($input_val))
				{
					$ids = array_keys($input_val);
			
					foreach($ids as $id)
					{
						if ($output_val[$id] != NULL)
						{
							// so no need to change..
							$diff_vars["table"][$data_var][$id] = 0;
						}
						else
						{
							$diff_vars["table"][$data_var]["changed"] = 1;
							$diff_vars["table"][$data_var][$id] = 1;
						}
					}
					if(compare_arrays($input_val, $output_val) == 1)
					{
						$diff_vars["table"][$data_var]["changed"] = 1;
					}
				}
			}
		}
	}
	$data_vars = array_keys($diff_vars["table"]);
	foreach($data_vars as $data_var)
	{
		if($diff_vars["table"][$data_var]["changed"] != 1)
		{
			if(count($output_vars["table"][$data_var]) != 0) //if the count == 0 no problem
			{
				$output_vars["table"][$data_var] = NULL;
				$diff_vars["table"][$data_var] = NULL;
			}
		}
	}
	
	// The following funciton is implemented on the generated php file..
	$diff_vars["table"] = update_databound_properties($diff_vars, $output_vars, 0);
	
	// now we have all the informations, just build the xml.. :))
	$doc = new DOMDocument('1.0', 'iso-8859-1');
	$root_ele = $doc->createElement("xml");
	$root_ele->setAttribute("id", "xmlDI");
	$doc->appendChild($root_ele);
	
	$data_ele = $doc->createElement("data");
	$root_ele->appendChild($data_ele);
				
	$table_ele = $doc->createElement("data");
	$table_ele->setAttribute("id","dataVars");
	$data_ele->appendChild($table_ele);
	$data_node_empty = true;
	if(is_array($diff_vars["table"]))
	{
		$table_vars = array_keys($diff_vars["table"]);
		if(count($table_vars) == 0)
		{
			$text_element = $doc->createTextNode("");
			$table_ele->appendChild($text_element);
		}
		foreach($table_vars as $table_var)
		{
			$var_element= $doc->createElement("var");
			$var_element->setAttribute("val", $table_var);
			$values = $diff_vars["table"][$table_var];
		
			if(count($diff_vars["table"][$table_var]) > 0)
			{
				create_xml_from($values, $var_element, $doc);
				$table_ele->appendChild($var_element);
				$data_node_empty = false; //at least one entry..
			}
			//elseif($diff_vars["table"][$table_var] != NULL) //just create an empty element
			elseif(is_array($diff_vars["table"][$table_var])) //just create an empty element
			{
				$text_element = $doc->createTextNode("");
				$var_element->appendChild($text_element);	
				$table_ele->appendChild($var_element);
				$data_node_empty = false; //at least one entry..		
			}
				
		}
	}
	if(!is_array($diff_vars["table"]) || $data_node_empty) //do what count == 0 do
	{
		$text_element = $doc->createTextNode("");
		$table_ele->appendChild($text_element);
	}
	//create_xml_from($table_vars, $table_ele, $doc);
	
	$page_ele = $doc->createElement("page");
	$page_ele->setAttribute("id","pageVars");
	$data_ele->appendChild($page_ele);
	
	$page_vars = array_keys($diff_vars["page"]);
	
	if(count($page_vars) == 0)
	{
		$text_element = $doc->createTextNode("");
		$page_ele->appendChild($text_element);
	}
	foreach($page_vars as $page_var)
	{
		$var_element= $doc->createElement("var");
		$var_element->setAttribute("name", "_".$page_var);
		$value = $diff_vars["page"][$page_var];
		$text_element = $doc->createTextNode($value);
		$var_element->appendChild($text_element);
		
		$page_ele->appendChild($var_element);
	}
	
	//duplicate that for system variables..
	
	$sys_ele = $doc->createElement("sys");
	$sys_ele->setAttribute("id","sysVars");
	$data_ele->appendChild($sys_ele);
	
	$sys_vars = array_keys($diff_vars["sys"]);
	if(count($sys_vars) == 0)
	{
		$text_element = $doc->createTextNode("");
		$sys_ele->appendChild($text_element);
	}
	foreach($sys_vars as $sys_var)
	{
		$var_element= $doc->createElement("var");
		$var_element->setAttribute("name", "_".$sys_var);
		$value = $diff_vars["sys"][$sys_var];
		$text_element = $doc->createTextNode($value);
		$var_element->appendChild($text_element);
		
		$sys_ele->appendChild($var_element);
	}
		
	//create_xml_from($diff_vars["page"], $page_ele, $doc);
	
	$data_to_send = $doc->saveXML($doc->documentElement);
	return $data_to_send;
}

function create_xml_from($var, $parent_ele, $doc)
{
	if(is_array($var))
	{
		$keys = array_keys($var);
		foreach ($keys as $key)
		{

			$key_ele = $doc->createElement("attr");
			$key_ele-> setAttribute("val", $key);
			$parent_ele->appendChild($key_ele);
			
			create_xml_from($var[$key], $key_ele, $doc);
		}
	}
	else
	{
		//just set text for this..
		$parent_ele->nodeValue = $var;
	}
}

function get_database_info()
{
	$project_doc = new DOMDocument();
	$project_xml = "../project.xml";
	$project_doc->load($project_xml);

	if($project_doc == NULL || $project_doc->documentElement == NULL)
	{
		return NULL;
	}
	
	$the_project = array();
	
	$child_nodes = $project_doc->documentElement->childNodes;
	foreach($child_nodes as $child_node)
	{
		if($child_node->tagName == "database")
		{
			$db_name = $child_node->getAttribute("name");
			$the_project["db_name"] = $db_name;
			
			$server_child_nodes = $child_node->childNodes;
			foreach($server_child_nodes as $server_child_node)
			{
				if($server_child_node->tagName == "password")
				{
					$db_password = getNodeValue($server_child_node);
					$the_project["db_password"] = $db_password;
				}
				elseif($server_child_node->tagName == "server")
				{
					$db_server = getNodeValue($server_child_node);
					$the_project["db_server"] = $db_server;
				}
			}
		}
	}
	
	return $the_project;
}

function getNodeValue($node)
{
	if($node->firstChild)
	{
		return $node->firstChild->nodeValue;
	}
	return "";
}

// return 1 at any differet..
function compare_arrays($input, $output)
{
	$input_keys = array_keys($input);
	$output_keys = array_keys($output);
	
	foreach($input_keys as $input_key)
	{
		$input_value = $input[$input_key];
		$output_value = $output[$input_key];
		if(is_array($input_value))
		{
			if(is_array($output_value))
			{
				$ret_val = compare_arrays($input_value, $output_value);
				if($ret_val == 1)
				{
					return 1;
				}
			}
			else //already different.., but very rare..
			{
			}
		}
		else
		{
			if(input_value != $output_value)
			{
				return 1;
			}
		}
	}
	return 0;
}

?>